import Vue from 'vue'
import Vuex from 'vuex';
Vue.use(Vuex);
import {
    userInfoAPI
} from '@/api'
import {
    saveToken,
    getToken,
    delToken
} from '@/utils/auth'
import {
    Toast
} from 'vant';
import router from '@/router'
const store = new Vuex.Store({
    state: {
        tokenObj: getToken() || {},
        userInfo: {}
    },
    // mutations写同步代码可以直接修改state里面的值
    mutations: {
        changeToken(state, obj) {
            state.tokenObj = obj
            saveToken(obj)
        },
        changeUserInfo(state, obj) {
            state.userInfo = obj
        },
        logout(state) {
            state.tokenObj = {}
            delToken()
            state.userInfo = {}
        }
    },
    //actions写异步代码步可以直接修改state里面的值,要通过commit提交来修改值
    actions: {
        async getUserInfo(context) {
            if (!context.state.userInfo.name) {
                let res = await userInfoAPI();
                context.commit("changeUserInfo", res.data.data);
            }
        }
    }
});
export default store